<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/24 0024
 * Time: 上午 9:49
 * Author: NanTao <260053530@qq.com>
 */

namespace app\index\controller;
use think\Db;
class Integral extends Common
{
    /*
     * 积分商城首页
     * */
    public function index()
    {
        $list = Db::name('integral')->where(['status'=>0])->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /*
     * 商品详情
     * */
    public function show()
    {
        $id = input('param.id');
        $info = Db::name('integral')->where('id',$id)->find();
        $this->assign('info',$info);
        $imglist = Db::name('img')->where(['type'=>2,'main_id'=>$id])->select();
        $this->assign('imglist',$imglist);
        return $this->fetch();
    }
    /*
     * 积分订单
     * */
    public function addOrder()
    {
        $goodsId = input('param.goodsId');
        $goodsCount = input('param.goodsCount');
        $goods = Db::name('integral')->find($goodsId);
        if ($goodsCount > $goods['stock'])
        {
            json_msg(1,'商品库存不足,无法兑换!!!');
        }
        $userId = session('userId');
        $userPoint = get_user_info_data($userId,'point');
        $count = $goods['sell_price']*$goodsCount;
        if ($userPoint < $count)
        {
            json_msg(1,'您的积分不足,无法兑换!!!');
        }
        $orderNo='JF'.date('ymdHis',time()).mt_rand(1000,9999);
        $data = [
            'order_no'=>$orderNo,
            'goods_title' => $goods['title'],
            'goods_img_url'=>$goods['img_url'],
            'goods_sell_price'=>$goods['sell_price'],
            'goods_id'=>$goodsId,
            'user_id'=>$userId,
            'goods_count'=>$goodsCount,
            'count'=>$count
        ];
        if (Db::name('integral_order')->insert($data))
        {
             $result = post_point($userId,$count,"兑换商品花费{$count}积分",2,'dec');
             Db::name('integral')->where('id',$goodsId)->setDec('stock',$goodsCount);
             if ($result)
             {
                 json_msg(0,'兑换成功');
             } else {
                 json_msg(1,'兑换失败');
             }
        } else {
            json_msg(1,'兑换失败');
        }
    }
    /*
     * 积分订单
     * */
    public function orders()
    {
        $userId = session('userId');
        $list = Db::name('integral_order')->where(['user_id'=>$userId,'is_del'=>0])->order('status asc,add_time desc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    /*
     * 积分日志
     * */
    public function log()
    {
        $typeModel = config('point_type_arr');
        $userId = session('userId');
        $list = Db::name('user_point')->where('user_id',$userId)->order('add_time desc')->select();
        foreach ($list as $k=>$v)
        {
            $list[$k]['typeshow'] = $typeModel[$v['type']];
        }
        $this->assign('list',$list);
        return $this->fetch();
    }
    /*
     * 删除
     * */
    public function del()
    {
        $orderId = input('param.orderId');
        $result = Db::name('integral_order')->where('id',$orderId)->delete();
        if ($result)
        {
            json_msg(0,'已删除');
        } else {
            json_msg(1,'系统错误,操作失败,请稍候再试');
        }
    }
}